package com.bigzero.workflow.common.controller;

import com.bigzero.workflow.common.dto.MenuNavigationDto;
import com.bigzero.workflow.common.model.R;
import com.bigzero.workflow.common.model.Route;
import com.bigzero.workflow.common.model.TokenAuthInfo;
import com.bigzero.workflow.modules.system.service.UserCacheService;
import com.bigzero.workflow.modules.system.vo.MenuVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 首页前端控制器
 * @author fanxinxiong
 * @since 2025-02-13
 */
@RestController
@RequestMapping("/home")
@Tag(name = "首页")
@Slf4j
public class HomeController {

    @Resource
    private UserCacheService userCacheService;

    /**
     * 路由列表集合
     * @param userDetails
     * @return List<MenuVo>
     */
    @Operation(summary = "路由列表集合", description = "路由列表集合")
    @PostMapping(value = "getRouteList")
    public R<List<Route>> getRouteList(@AuthenticationPrincipal UserDetails userDetails) {
        List<Route> routeList = new ArrayList<>();
        try{
            Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
            //details里面可能存放了当前登录用户的详细信息，也可以通过cast后拿到
            TokenAuthInfo tokenAuthInfo = (TokenAuthInfo) authentication.getDetails();
            if(tokenAuthInfo != null){
                routeList = userCacheService.getRouteListByTokenAuthInfo(tokenAuthInfo);
            }
        }catch(Exception e){
            log.error(e.getMessage());
        }
        return R.ok(routeList);
    }

    /**
     * 目录列表集合
     * @param userDetails
     * @return List<MenuVo>
     */
    @Operation(summary = "目录列表集合", description = "目录列表集合")
    @PostMapping(value = "getDirectoryVoList")
    public R<List<MenuVo>> getDirectoryVoList(@AuthenticationPrincipal UserDetails userDetails) {
        List<MenuVo> directoryVoList = new ArrayList<>();
        try{
            Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
            //details里面可能存放了当前登录用户的详细信息，也可以通过cast后拿到
            TokenAuthInfo tokenAuthInfo = (TokenAuthInfo) authentication.getDetails();
            if(tokenAuthInfo != null){
                directoryVoList = userCacheService.getDirectoryVoListByTokenAuthInfo(tokenAuthInfo);
            }
        }catch(Exception e){
            log.error(e.getMessage());
        }
        return R.ok(directoryVoList);
    }

    /**
     * 获取菜单集合
     * @param menuNavigationDto
     * @return
     */
    @Operation(summary = "获取菜单vo集合", description = "获取菜单vo集合")
    @PostMapping(value = "getMenuVoList")
    public List<MenuVo> getMenuVoList(@RequestBody MenuNavigationDto menuNavigationDto) {
        List<MenuVo> menuVoList = new ArrayList<>();
        try{
            String parentId = (menuNavigationDto.getParentId() != null) ? menuNavigationDto.getParentId() : "0";
            Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
            //details里面可能存放了当前登录用户的详细信息，也可以通过cast后拿到
            TokenAuthInfo tokenAuthInfo = (TokenAuthInfo) authentication.getDetails();
            if(tokenAuthInfo != null){
                menuVoList = userCacheService.getMenuVoListByTokenAuthInfo(tokenAuthInfo, parentId);
            }
        }catch(Exception e){
            log.error(e.getMessage());
        }
        return menuVoList;
    }

    /**
     * 获取权限标识集合
     * @return List<String>
     */
    @Operation(summary = "获取权限标识集合", description = "获取权限标识集合")
    @PostMapping(value = "getPermsList")
    public R<List<String>> getPermsList(@AuthenticationPrincipal UserDetails userDetails){
        List<String> permsList = new ArrayList<>();
        try{
            Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
            //details里面可能存放了当前登录用户的详细信息，也可以通过cast后拿到
            TokenAuthInfo tokenAuthInfo = (TokenAuthInfo) authentication.getDetails();
            if(tokenAuthInfo != null){
                permsList = userCacheService.getPermsListByTokenAuthInfo(tokenAuthInfo);
            }
        }catch(Exception e){
            log.error(e.getMessage());
        }
        return R.ok(permsList);
    }
}
